#ifndef  __AT24CXX_H__
#define	 __AT24CXX_H__ 


#include "main.h"
#include "i2c.h"


#define  I2C_HANDLE           hi2c2
#define  DEV_ADDR_24CXX       0x00A0

//#define  PAGE_SIZE_24CXX      (uint16_t) 16   //24CXX页包含字节大小
//#define  PAGE_NUM             (uint16_t) 256    //总页数
//#define  MEM_SIZE_24CXX       (uint16_t) 4096  //24cxx存储容量 Byte总数


#define  AT24C_TIMEOUT        200
//#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_16BIT
#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_8BIT

#define  _EEPRON_SIZE_KBIT  4
#define  PAGE_SIZE_24CXX  4*_EEPRON_SIZE_KBIT


#if(_EEPRON_SIZE_KBIT == 1) || (_EEPRON_SIZE_KBIT == 2)||(_EEPRON_SIZE_KBIT == 4) 
#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_8BIT
#define  PAGE_NUM             (uint16_t) 256    //总页数
#define  MEM_SIZE_24CXX       (uint16_t) _EEPRON_SIZE_KBIT*PAGE_NUM*4   //24cxx存储容量 Byte总数
#elif(_EEPRON_SIZE_KBIT == 8) || (_EEPRON_SIZE_KBIT == 16)
#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_16BIT
#define  PAGE_NUM             (uint16_t) 256    //总页数
#define  MEM_SIZE_24CXX       (uint16_t) _EEPRON_SIZE_KBIT*PAGE_NUM*4   //24cxx存储容量 Byte总数
#elif(_EEPRON_SIZE_KBIT == 32) || (_EEPRON_SIZE_KBIT == 64) 
#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_32BIT
#define  PAGE_NUM             (uint16_t) 256    //总页数
#define  MEM_SIZE_24CXX       (uint16_t) _EEPRON_SIZE_KBIT*PAGE_NUM*4   //24cxx存储容量 Byte总数
#elif(_EEPRON_SIZE_KBIT == 128) || (_EEPRON_SIZE_KBIT == 256) 
#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_64BIT
#define  PAGE_NUM             (uint16_t) 256    //总页数
#define  MEM_SIZE_24CXX       (uint16_t) _EEPRON_SIZE_KBIT*PAGE_NUM*4   //24cxx存储容量 Byte总数
#elif(_EEPRON_SIZE_KBIT == 512) 
#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_128BIT
#define  PAGE_NUM             (uint16_t) 256    //总页数
#define  MEM_SIZE_24CXX       (uint16_t) _EEPRON_SIZE_KBIT*PAGE_NUM*4   //24cxx存储容量 Byte总数
#elif(_EEPRON_SIZE_KBIT == 1024) 
#define  AT24C_MEMADD_SIZE    I2C_MEMADD_SIZE_256BIT
#define  PAGE_NUM             (uint16_t) 256    //总页数
#define  MEM_SIZE_24CXX       (uint16_t) _EEPRON_SIZE_KBIT*PAGE_NUM*4   //24cxx存储容量 Byte总数
#endif




//检查设备是否准备好
HAL_StatusTypeDef  AT24Cxx_IsDeviceReady(void);

//在任意地址写入1字节
HAL_StatusTypeDef  AT24Cxx_WriteOneByte(uint16_t memaddress , uint8_t byteData);

//在任意地址读出1字节
HAL_StatusTypeDef  AT24Cxx_ReadOneByte(uint16_t memaddress , uint8_t *byteData);


//连续读取数据，任意地址，任意长度，不受页控制
HAL_StatusTypeDef  AT24Cxx_ReadBytes(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen);

//限制在一个页内连续写入数据，最多写满1页，从任意地址开始，但起始地址+数据长度不能越过页的边界
HAL_StatusTypeDef  AT24Cxx_WriteInonePage(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen);

//写任意长数据，可以超过一页字节，但写入地址必须从首地址开始
HAL_StatusTypeDef  AT24Cxx_WriteLongData(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen);

//写任意长数据，可以超过一页字节，写入地址不必从首地址开始
HAL_StatusTypeDef AT24Cxx_Arbitrary_Write_Cmd(uint16_t memaddress , uint8_t *pBuffer,uint16_t bufferLen);

//擦除正片AT24Cxx
HAL_StatusTypeDef AT24Cxx_EarseAll_Cmd(void);


#endif

